/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.ui.base;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;

import com.reportgear.report.context.ReportContext;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * 线性选择框
 * 
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class JLineComboBox extends JComboBox {

	public JLineComboBox(int[] arrInt) {
		Integer[] arrTemp = new Integer[arrInt.length];
		for (int i = 0; i < arrInt.length; ++i) {
			arrTemp[i] = new Integer(arrInt[i]);
		}
		setModel(new DefaultComboBoxModel(arrTemp));
		setRenderer(new LineCellRenderer());
	}

	public int getSelectedLineStyle() {
		int i = ((Integer) getSelectedItem()).intValue();
		return ((i < 0) ? 0 : i);
	}

	public void setSelectedLineStyle(int index) {
		setSelectedItem(new Integer(index));
	}

	class LineCellRenderer extends DefaultListCellRenderer {

		private int style = 0;
		private boolean isSelected = false;

		@Override
		public Component getListCellRendererComponent(JList list, Object object, int index, boolean isSelected,
				boolean cellHasFocus) {
			this.style = ((Integer) object).intValue();
			this.isSelected = isSelected;
			return this;
		}

		@Override
		public void paint(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			Dimension dim = getSize();
			g2.setColor(Color.BLACK);
			g2.setFont(ReportContext.INSTANCE.getDefaultValues().getReportFont());
			FontMetrics fm = GraphHelper.getFontMetrics(ReportContext.INSTANCE.getDefaultValues().getReportFont());
			if (this.style == 0) {
				GraphHelper.drawString(g2, "无", 4.0D, (dim.height - fm.getHeight()) / 2 + fm.getAscent());
			} else {
				GraphHelper.drawLine(g2, 4.0D, dim.height / 2, dim.width - 8, dim.height / 2, this.style);
			}
			if (!(this.isSelected)) {
				return;
			}
			g2.setColor(Color.blue);
			GraphHelper.drawRect(g2, 0.0D, 0.0D, dim.width - 1, dim.height - 1);
		}

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(60, 16);
		}

		@Override
		public Dimension getMinimumSize() {
			return getPreferredSize();
		}
	}
}
